package com.lh.mvc;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lh.mvc.spitter.Person;
import com.lh.mvc.spitter.SpitterService;

@Controller
public class HomeController {
	private static final Logger logger = Logger.getLogger(HomeController.class);
	public static final int DEFAULT_SPITTERS_PER_PAGE = 25;
	
	private SpitterService spitterService;
	private String returnPage = "";

    @Autowired
    @Qualifier("person")
	private Person person;
 
	@Inject
	public HomeController(SpitterService spitterService) {
		this.spitterService = spitterService;
		logger.info("======HomeController.HomeController===");
	}

	
	@RequestMapping({"/","/home"})
	public String showHomePage(Model model) {
		logger.info("showHomePage======Model======"+model);
		Date today = new Date();
		model.addAttribute("persons", spitterService.getSearchAll());
		model.addAttribute("today", today);
		model.addAttribute("name", "lianghuao0000k");
		
		logger.info("======HomeController.showHomePage===/home==="+model);
		
		return "home";
	}

	@RequestMapping({"/userHome"})
	public String showUserHomePage(Model model) {
		logger.info("======showUserHomePage=====userDemo/userHome====");
		return "userDemo/userHome";
	}

	@RequestMapping({"/userLogin"})
	public String showUserLoginPage(Model model) {
		logger.info("======showUserLoginPage=====userDemo/userLogin====");
		return "userDemo/userLogin";
	}
	
	@RequestMapping(value ="/userLogin", method = RequestMethod.POST)
	public String showUserLoginPage(String loginName,String password,Model model) {

		logger.info("======HomeController.showLoginPage===/userLogin===");
		logger.info("======loginName==="+loginName+",  password=== "+password);
		model.addAttribute("loginName",loginName);
		logger.info("======model======"+model);
		
		if (loginName == null || "".equals(loginName)) {
			returnPage = "userDemo/userLogin";
		} else {
			model.addAttribute("persons", spitterService.getSearchAll());
			returnPage = "userDemo/userList";
		}
		model.addAttribute("loginName",loginName);
		return returnPage;
	}

	@RequestMapping({"/addUserPage"})
	public String showAddUserPage(HttpServletRequest request, Model model) {
		logger.info("======showAddUserPage=====userDemo/userLogin====");
		return "userDemo/addUser";
	}

	@RequestMapping({"/addUser"})
	public String doAddUser(HttpServletRequest request, Model model, RedirectAttributes attr) {
		Map<String, Object> resultMap = null;
		logger.info("======showUserLoginPage=====userDemo/userLogin====");

		person.setName(request.getParameter("name"));
		person.setAge(request.getParameter("age"));
		person.setSex(request.getParameter("sex"));
		person.setCity(request.getParameter("city"));

		logger.info("person    person===>" + person);
		
		resultMap = spitterService.addUser(person);

		logger.info("resultMap    resultMap===>" + resultMap);
		logger.info("request===>" + request);
		
		if (resultMap != null) {
			attr.addFlashAttribute("id", resultMap.get("id"));
			attr.addFlashAttribute("mode", "mode_addUser");
		} 
		return "redirect:/userFormPage";
	}
	

	@RequestMapping({"/userFormPage"})
	public String showUserFormPage(HttpServletRequest request, Model model) {
		Map<String, Object> resultMap = null;
		logger.info("======showUserFormPage=====/userFormPage     ====" + model);
		logger.info("====== id====" + request.getParameter("id"));
		Map<String, Object> paramMap = model.asMap();
		logger.info("====== paramMap   id====" + paramMap.get("id"));
		
		String id = request.getParameter("id");
		
		if (id == null || "null".equals(id)) {
			if (paramMap != null && paramMap.get("id") != null) {
				id = "" + paramMap.get("id");
			}
		}

		logger.info("====== ididid====" + id);
		if (id != null && !"null".equals(id)) {
			resultMap = spitterService.getUser(id);
		}
		
		logger.info("======resultMap  ====" + resultMap);
		
		if (resultMap != null) {
			model.addAttribute("person", resultMap);
		}

		logger.info("======model====" + model);

		model.addAttribute("from_mode", "" + request.getParameter("mode")); //goForm
		return "userDemo/userForm";
	}

	@RequestMapping({"/updateUser"})
	public String doUpdateUser(HttpServletRequest request, RedirectAttributes attr) {
		int result = 0;
		logger.info("======doUpdateUser=====doUpdateUser===="+request.getParameterMap());

		person.setId(request.getParameter("id"));
		person.setName(request.getParameter("name"));
		person.setAge(request.getParameter("age"));
		person.setSex(request.getParameter("sex"));
		person.setCity(request.getParameter("city"));

		logger.info("person    person===>" + person);
		
		result = spitterService.updateUser(person);

		logger.info("resultMap    resultMap===>" + result);
		
		if (result > 0) {
			attr.addFlashAttribute("id", request.getParameter("id"));
			attr.addFlashAttribute("message", "success");
		} 
		attr.addFlashAttribute("from_mode", "updateUser");
		return "redirect:/userFormPage";
	}

	@RequestMapping({"/userList","userDemo/userList"})
	public String showUserListPage(Model model) {
		logger.info("======showUserListPage====userList===="+model);

		model.addAttribute("persons", spitterService.getSearchAll());
		logger.info("======showUserListPage=====model  after===="+model);
		return "userDemo/userList";
	}


	@RequestMapping({"/deleteUsers"})
	public String doDeleteUsers(HttpServletRequest request, Model model, RedirectAttributes attr) {
		logger.info("======doDeleteUsers=====/deleteUsers====");

		String[] checkedIds = request.getParameterValues("checkId");

		logger.info("======   checkIds   ===="+checkedIds);

		spitterService.deleteUsers(checkedIds);
		attr.addFlashAttribute("message", "Delete success!");
		attr.addFlashAttribute("from_mode", "deleteUsers");

		return "redirect:/userList";
	}
	
	
}
